<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.lmpop.trade.ti_attach.*" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
			
	String attach_root_id = "",img_type="1";
	if(request.getParameter("attach_root_id")!=null){
		attach_root_id = request.getParameter("attach_root_id");
	} 
	//限制上传图片个数。1：限制一个 2：不限制 3:只可查看
	if(request.getParameter("img_type")!=null){ 
		img_type = request.getParameter("img_type");
	}
	Ti_attachInfo attachInfo = new Ti_attachInfo();
	List aList = attachInfo.getListByAttachrootid(attach_root_id);
	
	String upload_style="";
	if(img_type.equals("1")){
		if(aList != null && aList.size() > 0){
			upload_style="display:none;";
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="<%=basePath%>">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<link href="/program/plugins/upload/uploadImg/uploadify.css" type="text/css" rel="stylesheet" />
		<script type="text/javascript" src="/program/plugins/upload/uploadImg/jquery-1.4.2.min.js"></script>
		<script type="text/javascript" src="/program/plugins/upload/uploadImg/swfobject.js"></script>
		<script type="text/javascript" src="/program/plugins/upload/uploadImg/jquery.uploadify.v2.1.4.min.js"></script>
		<style type="text/css">
		.del_button{cursor:pointer;background:#e9cc59;border-color: #FFFF99 #996600 #996600 #FFFF99;border-style: solid; 
	  		 border-width: 1px;height: 25px;width: 60px; line-height:25px; color:#000; font-weight:bold; float:left; text-align:center; margin-left:35px;}
		</style>
		<script type="text/javascript"> 
		//   
        jQuery.noConflict();
		//
		jQuery(document).ready(function() {
			jQuery('#file_upload').uploadify( {
				'uploader' : '/program/plugins/upload/uploadImg/uploadify.swf',
				'buttonImg'   : '/program/images/uploader.gif',
				'script' : '/servlet/upload',
				'cancelImg' : '/program/plugins/upload/uploadImg/cancel.png',
				'folder' : 'uploads',
				'queueID' : 'fileQueue',
				'scriptData':{'img_type':'<%=img_type%>','attach_root_id':'<%=attach_root_id%>'},
				'auto' : true,
				'multi' : false,
				'simUploadLimit' : 1,
				'buttonText' : '浏览',
				'method':'GET',//如果向後台傳送數據必須為GET
				'sizeLimit': 10*1024*1024,
				'queueSizeLimit' : 5,
				'onComplete' : function(event, ID, fileObj, response, data) {
					if(response == '333'){
						alert("图片格式错误！");
					} else {
						var resArr = response.split("|");
						var img_path = resArr[0];
						var attach_id = resArr[1];
						jQuery("#displayImg").append("<div id='"+attach_id+"'><img src='"+img_path+"'width='100px' height='100px'/></br><a hrf='#' class='del_button'  onclick=deleteTheImg('"+attach_id+"') style='cursor:pointer;'>删除</a></span></div>");
						jQuery("#displayImg").show();	
						if('<%=img_type%>' == '1'){
						  jQuery("#hidden_upload_bar").hide();
						}	  
				    }
					$("#avatar_is_not").hide();
					originalAction(img_path);
			  }
			} );
		});
		//
		function deleteTheImg(attach_id){
			var dataValue = 'attach_id='+attach_id + '&attach_root_id=<%=attach_root_id%>'+'&img_type=<%=img_type%>';
			jQuery.ajax({
			   type: 'POST',
			   url: '/program/plugins/upload/uploadImg/deleteImg.jsp',
			   data: dataValue,		   
			   success: function(data){
					jQuery("#hidden_img_data").html(data);
					var is_delete = jQuery("#hidden_delete_remote").text();
					if(is_delete == '1'){
						jQuery("#"+attach_id+"").hide();
					}					
					var check_img = jQuery("#hidden_size_remote").text();
					if(check_img == '0'){
						jQuery("#hidden_upload_bar").show();
					}
					$("#avatar_is_not").show();
					originalAction("/templets/html/b2c/images/touxiang.png");
			   }
		   });  
		}	
		//改变头像成功动作
		function originalAction(url) {
			$("#avatar_img_contaner").attr("src",url);
		}   
        </script>
	</head>
	<body>
		<div id="displayImg">
		<%
			if(aList!=null && aList.size() > 0 ){
				Map imgMap = new Hashtable();
				String img_path = "";
				String attach_id="";
				for(int i=0;i < aList.size(); i++){
					imgMap = (Hashtable)aList.get(i);
						if(imgMap != null && imgMap.size() >0){
							if(imgMap.get("file_path") != null){
								img_path = imgMap.get("file_path").toString();
								attach_id = imgMap.get("attach_id").toString();
								%>
								<div id="<%=attach_id%>">
								<img src='<%=img_path%>'width='100px' height='100px'/></br><span><a href="#" class="del_button" onClick="deleteTheImg('<%=attach_id%>')">删除</a></span>
								</div>	
							   <%
							}
						}		
				}
			}
		%>
		</div>
		<div id="avatar_is_not" style="<%=upload_style%>"><img src='/templets/html/b2c/images/touxiang.png' width='100px' height='100px'/></div>
		</br>
		<div style="<%=upload_style%>" id="hidden_upload_bar"><input id="file_upload" name="file_upload" type="file" /></div>
		<div style="display:none;" id="hidden_img_data"></div>
		<input type="hidden" name="attach_root_id" value="<%=attach_root_id%>" />
		<input type="hidden" name="attach_sign" value="insert" />
	</body>
</html>